package day3;

import java.util.Collection;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

/**
 * ClassName：Test3
 *
 * @Author：小鱼
 * @Create：2025-08-13 16:04
 */
public class Test3 {
    public static void main(String[] args) {
        //创建Teacher对象
        Teacher t1 = new Teacher("005","aa",28);
        Teacher t2 = new Teacher("001","bb",30);
        Teacher t3 = new Teacher("002","cc",28);
        Teacher t4 = new Teacher("004","aa",32);
        Teacher t5 = new Teacher("003","aa",28);

        //Comparable接⼝实现（自然排序）
        Set<Teacher> set1 = new TreeSet<>();
        set1.add(t1);
        set1.add(t2);
        set1.add(t3);
        set1.add(t4);
        set1.add(t5);
        System.out.println("Comparable接⼝实现（自然排序）结果为："+set1);

        //Comparator接⼝实现（比较器排序）
        Comparator<Teacher> comparator = new Comparator<Teacher>() {
            @Override
            public int compare(Teacher o1, Teacher o2) {
                int num;
                // name进行升序排序
                num=o1.getName().compareTo(o2.getName());
                if (num==0){
                    //名字相同的时候通过年龄进⾏升序排序
                    num = o1.getAge()-o2.getAge();
                }
                if(num==0){
                    //年龄相同时通过id升序排序。
                    num=o1.getId().compareTo(o2.getId());
                }
                return num;
            }
        };
        Set<Teacher> set2 = new TreeSet<>(comparator);
        set2.add(t1);
        set2.add(t2);
        set2.add(t3);
        set2.add(t4);
        set2.add(t5);
        System.out.println("Comparator接⼝实现（比较器排序）："+set2);
    }
}
